<?php

namespace App\Http\Controllers;

use App\Models\Appointment;
use App\Models\ClinicCategory;
use App\Models\Doctor;
use App\Models\Feedback;
use App\Models\Mydoctor;
use App\Models\Reward;
use App\Models\WorkRoomSortCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\BaseController;
use PhpParser\Node\Stmt\If_;
use App\Util\Tool;

class DoctorController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $param  = $request->input();
        $res = (new Doctor())->getDoctorList($param);
        return  $this->ResultJson(1,'获取成功!',$res);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function cateList(Request $request)
    {
        $list = (new ClinicCategory())->where([['pid','=',0]])->get();
        if(!empty($list)){
            return  $this->ResultJson(1,'获取成功!',$list->toArray());
        }
        return [];
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 意见反馈
     */
    public function addFeedBack(Request $request)
    {
        $param = $request->input();
        if (empty($param['back_content'])) {
            return $this->ResultJson(0, '反馈内容不能为空!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户id不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        if($param['type'] == 1){
            if (empty($param['doctor_id'])) {
                return $this->ResultJson(0, '医师id不能为空!', []);
            }
        }else{
            if (empty($param['phone'])) {
                return $this->ResultJson(0, '电话不能为空!', []);
            }
           $res  =  Tool::checkMobile($param['phone']);
            if(!$res){
                return $this->ResultJson(0, '电话格式不正确!', []);
            }
        }
        $course = (new Feedback())->addFeedBack($param);
        if($course ===true){
            return  $this->ResultJson(1,'反馈成功!',[]);
        }
        return  $this->ResultJson(0,$course,[]);

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Doctor  $doctor
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $param = $request->input() ?: [];
        if(empty($param['doc_id'])){
            return  $this->ResultJson(0,'医师ID不能为空!',[]);
        }
        $course = (new Doctor())->getDoctorInfo($param);
        if(empty($course)){
            return  $this->ResultJson(0,'该医生不存在!',$course);
        }
        return  $this->ResultJson(1,'获取成功!',$course);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加我的医师
     */
    public function addMyDoctor(Request $request)
    {
        $param = $request->input();
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, '医生ID不能为空!', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new Mydoctor())->addMyDoctor($param);
        if ($res == 3) {
            return $this->ResultJson(1, '添加成功!', ["code"=>1]);
        } else if ($res == 1) {
            return $this->ResultJson(1, '删除我的医师成功!', ["code"=>2]);
        } else {
            return $this->ResultJson(0, '添加失败!', []);
        }
    }


    public function getMyDoctorList(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $res = (new Mydoctor())->getMyDoctorList($param);
        return $this->ResultJson(1, '获取成功!', $res);
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Doctor  $doctor
     * @return \Illuminate\Http\Response
     * 预约
     */
    public function makeAppointment(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        if (empty($param['phone'])) {
            return $this->ResultJson(0, '电话不能为空!', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空!', []);
        }
        if (empty($param['doc_id'])) {
            return $this->ResultJson(0, '医师ID不能为空!', []);
        }
        if (empty($param['rever_date'])) {
            return $this->ResultJson(0, '日期不能为空!', []);
        }
        if (empty($param['rever_time'])) {
            return $this->ResultJson(0, '时间不能为空!', []);
        }
        if(strtotime($param['rever_date'].' '.$param['rever_time']) < time()){
            return $this->ResultJson(0, '您预约的时间已过!', []);
        }
        if (empty($param['price'])) {
            return $this->ResultJson(0, '价格不能为空!', []);
        }
        $res = (new Appointment())->addAppointment($param);
        if (is_array($res)) {
            return $this->ResultJson(1, '预约成功!', $res);
        } else {
            return $this->ResultJson(0, $res, []);
        }

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Doctor  $doctor
     * @return \Illuminate\Http\Response
     * 增加咨询数
     */
    public function addConsult(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空", []);
        }
        $res = (new Doctor())->addConsult($param);
        if ($res) {
            return $this->ResultJson(1, "成功", []);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Doctor  $doctor
     * @return \Illuminate\Http\Response
     */
    public function addReward(Request $request)
    {
        $param = $request->input();
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空", []);
        }
        if (empty($param['money'])) {
            return $this->ResultJson(0, "金额不能为空", []);
        }
        $res = (new Reward())->addReward($param);
        if ($res) {
            return $this->ResultJson(1, "打赏成功", []);
        }
    }


    /***
     * 医护管理
     */
    public function getWorkDoctorList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空", []);
        }
        $res = (new Doctor())->getWorkDoctorList($param);
        if ($res) {
            return $this->ResultJson(1, "获取成功!", $res->toArray());
        }
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 新增子账号
     */

    public function addDoctor(Request $request)
    {
        $res = $request->input();
        if (!isset($res['id']) && empty($res['id'])) {
            $param = $this->verifyDoctor($res);
        } else {
            $param = true;
        }
        if ($param === true) {
            $result = (new Doctor())->addDoctor($res);
            if ($result === true) {
                if (isset($res['id']) && !empty($res['id'])) {
                    return $this->ResultJson(1, '编辑成功!', []);
                } else {
                    return $this->ResultJson(1, '添加成功!', []);
                }
            } else {
                return $this->ResultJson(0, $result, []);
            }
        }
        return $this->ResultJson(0, $param, []);;
    }

    /****
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 数据验证
     */
    public function verifyDoctor($res)
    {
        if (empty($res['phone']) || !isset($res['phone'])) {
            return '手机号不能为空!';
        }
        //判断验证码不能为空
//        if (empty($res['code'])) {
//            return '验证码不能为空!';
//        }
//        //判断短信验证码是否过期
//        $code = Tool:: GetSmsvalidator($res['phone']);
//        if (empty($code)) {
//            return '短信验证码已经过期';
//        }
//        //需要检查短信验证码是正确
//        if ($res['code'] != $code) {
//            return  '短信验证码错误';
//        }
//        //更新验证码为验证过了
//        $this->SetCode($res['phone'], $code);

        if (empty($res['account'])) {
            return '账号不能为空!';
        }
        if (empty($res['password'])) {
            return '密码不能为空!';
        }
        if (empty($res['real_name'])) {
            return '医护姓名不能为空!';
        }
        if (empty($res['idcard'])) {
            return '身份证号不能为空!';
        }
        if (empty($res['clinic_id'])) {
            return '科室ID不能为空!';
        }
        if (empty($res['professional_id'])) {
            return '职称ID不能为空!';
        }
        if (empty($res['hospital'])) {
            return '所属医院不能为空!';
        }
        if (empty($res['front_card'])) {
            return '身份证正面不能为空!';
        }
        if (empty($res['behind_card'])) {
            return '身份证反面不能为空!';
        }
        if (empty($res['profile'])) {
            return '头像不能为空!';
        }
        if (empty($res['certificate'])) {
            return '职业资质证书不能为空!';
        }
        if (empty($res['introduce'])) {
            return '简介不能为空!';
        }
        if (empty($res['skill'])) {
            return '擅长不能为空!';
        }
        return true;
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取工作室负责人信息
     */
    public function responsiblePerson(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空", []);
        }
        $res = (new Doctor())->getResponsiblePerson($param);
        return $this->ResultJson(1, '获取成功!', $res->toArray());
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 删除医师
     */
    public function deleteDoctor(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, "医师ID不能为空", []);
        }
        $res = (new Doctor())->deleteDoctor($param);
        if($res === true){
            return $this->ResultJson(1, '删除成功!',[]);
        }
        return $this->ResultJson(0, $res,[]);
    }

    /***
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 修改密码
     */
    public function updatePassword(Request $request)
    {
        $param = $request->input();
        if (empty($param['id'])) {
            return $this->ResultJson(0, "医师ID不能为空", []);
        }
        //判断手机号码不能为空
        if (empty($request->phone)) {
            return $this->ResultJson(0, '手机号码不能为空!', []);
        }
        //判断验证码不能为空
        if (empty($request->code)) {
            return $this->ResultJson(0, '验证码不能为空!', []);
        }
        //判断短信验证码是否过期
        $code = Tool:: GetSmsvalidator($request->phone);
        if (empty($code)) {
            return $this->ResultJson(0, '短信验证码已经过期', []);
        }
        //需要检查短信验证码是正确
        if ($request->code != $code) {
            return $this->ResultJson(0, '短信验证码错误', []);
        }
        //更新验证码为验证过了
        $this->SetCode($request->phone, $code);
        if (empty($param['password'])) {
            return $this->ResultJson(0, "密码不能为空", []);
        }
        if (empty($param['confirm_password'])) {
            return $this->ResultJson(0, "确认密码不能为空", []);
        }
        if ($param['password'] != $param['confirm_password']) {
            return $this->ResultJson(0, "两次输入密码不相同", []);
        }
        $res = (new Doctor())->updatePassword($param);
        if ($res === true) {
            return $this->ResultJson(1, '修改成功!', []);
        }
        return $this->ResultJson(0, '修改失败!', []);
    }

    /***
     * @param Request $request
     * 添加工作室自己的分类
     */
    public function addSelfCategory(Request $request)
    {
        $param = $request->input();
        if (empty($param['classify_name'])) {
            return $this->ResultJson(0, "分类名称不能为空", []);
        }
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空", []);
        }
        $res = (new WorkRoomSortCategory())->addSelfCategory($param);
        if ($res === true) {
            if(isset($param['id'])){
                return $this->ResultJson(1, '编辑成功!', []);
            }
            return $this->ResultJson(1, '添加成功!', []);
        }
        return $this->ResultJson(0, '添加失败!', []);
    }
    
    
}
